package com.ynwa.thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gzd
 * @date 2018-07-07 下午8:11
 * @desc 重入锁的公平锁与非公平锁
 */
public class ReenterTryLockFair {
    // 分别设置公平锁和非公平锁，分析打印结果
    private static ReentrantLock lock = new ReentrantLock(false);

    private static Runnable runnable = () -> {
        while (true) {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + " 获取了锁");
            } finally {
                lock.unlock();
            }
        }
    };

    public static void main(String[] args) {
        Thread thread1 = new Thread(runnable, "thread---1");
        Thread thread2 = new Thread(runnable, "thread---2");
        Thread thread3 = new Thread(runnable, "thread---3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

